angular.module('bank-account-list', [])

.controller 'BankAccountList', ($scope, $state, $http,$ionicPopup, $ionicHistory, $stateParams,userData,showToast) ->
    _.extend $scope,
        goBack:->
            $ionicHistory.goBack()
        addNewAccount:->
            $state.go 'tab.bank-account-edit'
        goEdit:(theItem)->
            $state.go 'tab.bank-account-edit',{account:theItem}
        remove:(item)->
            $ionicPopup.confirm({
                title:'确定要删除该银行账户吗?'
                okText:'确定'
                cancelText:'取消'
            }).then (res) ->
                if res
                    $http.get('/seller/',params:{action:'delshopbank',id:$scope.itemId,bankid:item.bankid,userid:userData.getToken()})
                    .success (resp) ->
                        if resp?.status == '0'
                            $scope.accounts=_.without($scope.accounts,item)
                            if $scope.accounts?.length==0
                                $scope.noAccount=true
                            else
                                $scope.noAccount=false
                            showToast '银行账户删除成功'
                        else
                            showToast resp.error
                    .error ->
                        showToast '服务器处理失败'
    loadAccounts=()->
        $http.get('/seller/',params:{action:'getbanklist',id:$scope.itemId,userid:userData.getToken()})
        .success (resp) ->
            console.log resp
            if resp?.status == '0'
                $scope.accounts=resp.banklist
                if $scope.accounts?.length==0
                    $scope.noAccount=true
                else
                    $scope.noAccount=false
            else
                angular.noop()
        .error ->
            showToast '服务器处理失败'
    $scope.$on '$ionicView.enter',->
        $scope.noAccount=undefined
        loadAccounts()
    $scope.$on '$IonicView.leave',->
        $scope.noAccount=undefined